import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import request from "@/utils/request.js";
import useAccountStore from "@/store/modules/AccountStore.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import {post} from "@/utils/http.js";

export const getOrderPageAPI = (current, pageSize, kw = null, status = null) => {
    const desKey = genDesKey()
    return request.get(
        '/order/page',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey),
                current, pageSize,
                status: status === null || status === '' ? null : status,
                kw: kw === null || kw === '' ? null : rsaPubEncrypt(kw),
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            // elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateOrderAPI = (orderId, status) => {
    return post(
        '/order/update',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const cancelOrderAPI = (orderId, status) => {
    return post(
        '/order/cancel',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const deliveryOrderAPI = (orderId, status, logistics) => {
    return post(
        '/order/delivery',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status,
            logistics: rsaPubEncrypt(logistics)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}